using Digitalmes.Application.Mes.Reworks.Doc.Commands;
using Digitalmes.Application.Mes.Reworks.Doc.Queries;

namespace Digitalmes.WebApi.Endpoints.Mes.Reworks;

/// <summary>
/// 产品返工终结点。
/// </summary>
public sealed class ReworkEndpoint : EndpointGroupBase
{
    public override void Map(WebApplication app)
    {
        app.MapGroup(this, "mes/rework", "doc")
            .RequireAuthorization()
            .MapGet(GetReworkDocPagedListAsync, "list-page")
            .MapGet(GetReworkablePagedListAsync, "reworkable/list-page")
            .MapGet(GetOnlineDoingReworkDocPagedListAsync, "online-doing/list-page")
            .MapPost(CreateOnlineReworkDocAsync, "online")
            .MapPut(UpdateOnlineReworkDocAsync, "online")
            .MapPut(CancelReworkDocAsync, "cancel/{id}");
    }

    [EndpointDescription("分页获取返工单信息")]
    public async Task<IApiResult> GetReworkDocPagedListAsync(ISender sender, [AsParameters] GetReworkDocListWithPagedQuery query)
    {
        var result = await sender.Send(query);
        return result.ToApiResult();
    }

    [EndpointDescription("分页获取可返工的产品信息")]
    public async Task<IApiResult> GetReworkablePagedListAsync(ISender sender, [AsParameters] GetReworkableListWithPagedQuery query)
    {
        var result = await sender.Send(query);
        return result.ToApiResult();
    }

    [EndpointDescription("分页获取在线返未完成的工单信息")]
    public async Task<IApiResult> GetOnlineDoingReworkDocPagedListAsync(ISender sender, [AsParameters] GetOnlineDoingReworkDocListWithPagedQuery query)
    {
        var result = await sender.Send(query);
        return result.ToApiResult();
    }

    [EndpointDescription("创建在线返工单")]
    public async Task<IApiResult> CreateOnlineReworkDocAsync(ISender sender, CreateOnlineReworkDocCommand dto)
    {
        var result = await sender.Send(dto);
        return result.ToApiResult();
    }

    [EndpointDescription("更新在线返工单")]
    public async Task<IApiResult> UpdateOnlineReworkDocAsync(ISender sender, UpdateOnlineReworkDocCommand dto)
    {
        var result = await sender.Send(dto);
        return result.ToApiResult();
    }

    [EndpointDescription("取消返工单")]
    public async Task<IApiResult> CancelReworkDocAsync(ISender sender, long id)
    {
        var result = await sender.Send(new CancelReworkDocCommand(id));
        return result.ToApiResult();
    }
}
